package com.bobbygu.server.simpleapp.controller;

import com.bobbygu.server.simpleapp.mapper.PluginMapper;
import com.bobbygu.server.simpleapp.model.PluginModel;
import com.bobbygu.server.simpleapp.model.response.BaseResponse;
import com.bobbygu.server.simpleapp.model.response.ConstResponse;
import com.bobbygu.server.simpleapp.model.response.ResultResponse;
import com.bobbygu.server.simpleapp.model.response.StringResponse;
import com.bobbygu.server.simpleapp.utils.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.ServletException;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;

@RestController
public class PluginController {
    @SuppressWarnings("SpringJavaAutowiringInspection")
    @Autowired
    private PluginMapper pluginMapper;
    private final String appName = "bobbyapp";

//    @RequestMapping(value = "/home/CheckVersion.do", method = RequestMethod.POST)
//    public BaseResponse checkVersion(@RequestBody Map<String, Object> requestMap) {
//        System.out.println("CheckVersion.do");
//        if (requestMap.get("VersionCode") == null) {
//            return new BaseResponse("VersionCode 不能为空", ConstResponse.STATUS_PARAM_ERROR);
//        }
//        if (requestMap.get("VersionName") == null) {
//            return new BaseResponse("VersionName 不能为空", ConstResponse.STATUS_PARAM_ERROR);
//        }
//        if (requestMap.get("SystemVersion") == null) {
//            return new BaseResponse("SystemVersion 不能为空", ConstResponse.STATUS_PARAM_ERROR);
//        }
//        if (requestMap.get("SystemType") == null) {
//            return new BaseResponse("SystemType 不能为空", ConstResponse.STATUS_PARAM_ERROR);
//        }
//
//        int versionCode = Integer.parseInt(requestMap.get("VersionCode").toString());
//        String versionName = requestMap.get("VersionName").toString();
//        String systemVersion = requestMap.get("SystemVersion").toString();
//        String systemType = requestMap.get("SystemType").toString();
//
//        VersionModel versionModel = pluginMapper.getPluginMsg(appName);
//        versionModel.isForce = false;
//        //请求者使用版本不是最新版本
//        if (versionModel.currentVersion > versionCode) {
//            if (versionModel.minSupportVersion > versionCode) {
//                //需要强制更新版本
//                versionModel.isForce = true;
//                return new ResultResponse(versionModel, ConstResponse.DESC_OK, ConstResponse.STATUS_OK);
//            } else {
//                //普通更新版本
//                return new ResultResponse(versionModel, ConstResponse.DESC_OK, ConstResponse.STATUS_OK);
//            }
//        } else {
//            //已经是最新版本
//            return new ResultResponse(versionModel, ConstResponse.DESC_OK, ConstResponse.STATUS_OK);
//        }
//    }

    @RequestMapping(value = "/home/GetPluginMsg.do", method = RequestMethod.POST)
    public BaseResponse getPluginMsg() {
        System.out.println("GetPluginMsg.do");
        ArrayList<PluginModel> pluginList = pluginMapper.getPluginMsg();
        System.out.println(pluginList);
        return new ResultResponse(pluginList, ConstResponse.DESC_OK, ConstResponse.STATUS_OK);
    }

    @RequestMapping(value = "/home/UpdatePluginMsg.do", method = RequestMethod.POST)
    public BaseResponse updatePluginMsg(MultipartHttpServletRequest multiReq) throws IOException, ServletException {
        System.out.println("UpdatePluginMsg.do");
        String rootPath = multiReq.getSession().getServletContext().getRealPath("/");
        final String ROOT_FILE_FOLDER = "fileDir";
        final String ROOT = rootPath + ROOT_FILE_FOLDER;

        Iterator<String> itr = multiReq.getFileNames();
        while (itr.hasNext()) {
            String name = itr.next();
            FileInputStream fs = (FileInputStream) multiReq.getPart(name).getInputStream();
            PluginModel pluginModel = pluginMapper.findPluginByName(name);
            try {
                Path path = Paths.get(ROOT, name);
                System.out.println(ROOT);
                System.out.println(path.toAbsolutePath().toString());
                if (!path.toFile().exists())
                    FileUtils.createFile(path.toString());
                Files.delete(path);
                Files.copy(fs, path);
                System.out.println(" Files.copy success!");
            } catch (IOException e) {
                e.printStackTrace();
                System.out.println(" Files.copy fail!");
            }

            if (pluginModel != null) {
                pluginMapper.updatePluginMsg(name, ROOT_FILE_FOLDER);
            } else {
                pluginMapper.insertPluginMsg(name, ROOT_FILE_FOLDER);
            }
        }

        return new ResultResponse(new StringResponse("上传成功"), ConstResponse.DESC_OK, ConstResponse.STATUS_OK);
    }

    @RequestMapping(value = "/home/DeletePluginByName.do", method = RequestMethod.POST)
    public BaseResponse deletePluginByName(@RequestBody Map<String, Object> requestMap) {
        System.out.println("DeletePluginByName.do");
        if (requestMap.get("Name") == null) {
            return new BaseResponse("Name 不能为空", ConstResponse.STATUS_PARAM_ERROR);
        }
        String name = requestMap.get("Name").toString();
        if (name != null) {
            pluginMapper.deletePluginByName(name);
            return new BaseResponse(ConstResponse.DESC_OK, ConstResponse.STATUS_OK);
        } else {
            return new BaseResponse(ConstResponse.DESC_PARAM, ConstResponse.STATUS_PARAM_ERROR);
        }
    }
}